﻿define(["jquery"], function ($) {
    return {
        init: function () {
            $("#AppId").unbind("change").bind("change", function () {
                var $this = $(this);
                var appId = parseInt($this.val());
                if (appId == 0) {
                    //重置
                    $("#EventId").empty();
                    $('#EventId').append("<option value='0'>请选择</option>");
                    $("#ActionId").empty();
                    $('#ActionId').append("<option value='0'>请选择</option>");
                    return;
                }
                $.ajax({
                    url: '/Rule/GetEventsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'appId': appId },
                    success: function (result) {
                        $("#EventId").empty();
                        $('#EventId').append("<option value='0'>请选择</option>");
                        $.each(result.events, function (index, value) {
                            $('#EventId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });

                        //重置
                        $("#ActionId").empty();
                        $('#ActionId').append("<option value='0'>请选择</option>");
                    }
                });
            });

            $("#EventId").unbind("change").bind("change", function () {
                var $this = $(this);
                var eventId = parseInt($this.val());
                if (eventId == 0) {
                    //重置
                    $("#ActionId").empty();
                    $('#ActionId').append("<option value='0'>请选择</option>");
                    return;
                }
                $.ajax({
                    url: '/Rule/GetActionsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'eventId': eventId },
                    success: function (result) {
                        $("#ActionId").empty();
                        $('#ActionId').append("<option value='0'>请选择</option>");
                        $.each(result.actions, function (index, value) {
                            $('#ActionId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });
                    }
                });
            });

            $("#ActionId").unbind("change").bind("change", function () {
                var $this = $(this);
                var actionId = parseInt($this.val());
                if (actionId == 0) return;
                $.ajax({
                    url: '/Rule/GetParamsAjax/',
                    cache: false,
                    type: 'post',
                    data: { 'actionId': actionId },
                    success: function (result) {
                        $("#ParamId").empty();
                        $('#ParamId').append("<option value='0'>请选择</option>");
                        $.each(result.paramInfos, function (index, value) {
                            $('#ParamId').append("<option value='" + value.id + "'>" + value.name + "</option>");
                        });
                    }
                });
            });
        }
    };
});